// $ANTLR 2.7.6 (2005-12-22): "logicExpression.g" -> "LogicExpression.java"$

package com.aote.expression.antlr;
import java.util.Map;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.aote.util.ComponentHelper;

import antlr.TokenBuffer;
import antlr.TokenStreamException;
import antlr.TokenStreamIOException;
import antlr.ANTLRException;
import antlr.LLkParser;
import antlr.Token;
import antlr.TokenStream;
import antlr.RecognitionException;
import antlr.NoViableAltException;
import antlr.MismatchedTokenException;
import antlr.SemanticException;
import antlr.ParserSharedInputState;
import antlr.collections.impl.BitSet;

public class LogicExpression extends antlr.LLkParser       implements LogicExpressionTokenTypes
 {

	private boolean getBoolean(String sbool) {
    	if(sbool.equals("true")) {
    		return true;
    	}
    	return false;
    }
    
    private String getValue(HTMLBasicComponent comp,Map map,String ID) {
       if(map==null){
    	  return ComponentHelper.getAttribute(comp,ID);
    	}else{
    	  return (String)map.get(ID);
    	}
    }
    
    private String getStringValue(String str) {
    	int len = str.length();
    	return str.substring(1, len - 1);
    }

protected LogicExpression(TokenBuffer tokenBuf, int k) {
  super(tokenBuf,k);
  tokenNames = _tokenNames;
}

public LogicExpression(TokenBuffer tokenBuf) {
  this(tokenBuf,2);
}

protected LogicExpression(TokenStream lexer, int k) {
  super(lexer,k);
  tokenNames = _tokenNames;
}

public LogicExpression(TokenStream lexer) {
  this(lexer,2);
}

public LogicExpression(ParserSharedInputState state) {
  super(state,2);
  tokenNames = _tokenNames;
}

	public final boolean  expression(
		HTMLBasicComponent comp,Map map
	) throws RecognitionException, TokenStreamException {
		boolean result=false;
		
		
			boolean b;
		
		
		try {      // for error handling
			result=comparator(comp,map);
			{
			_loop5:
			do {
				switch ( LA(1)) {
				case AND:
				{
					{
					match(AND);
					b=comparator(comp,map);
					}
					result = result && b;
					break;
				}
				case OR:
				{
					{
					match(OR);
					b=comparator(comp,map);
					}
					result = result || b;
					break;
				}
				default:
				{
					break _loop5;
				}
				}
			} while (true);
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_0);
		}
		return result;
	}
	
	public final boolean  comparator(
		HTMLBasicComponent comp,Map map
	) throws RecognitionException, TokenStreamException {
		boolean result=false;
		
		
			String a, b;
		
		
		try {      // for error handling
			if ((LA(1)==STRING||LA(1)==ID) && (LA(2)==EQUAL)) {
				a=item(comp,map);
				match(EQUAL);
				b=item(comp,map);
				result = a.equals(b);
			}
			else if ((LA(1)==STRING||LA(1)==ID) && (LA(2)==NOTEQUAL)) {
				a=item(comp,map);
				match(NOTEQUAL);
				b=item(comp,map);
				result=!a.equals(b);
			}
			else if ((LA(1)==STRING||LA(1)==ID) && (LA(2)==GT)) {
				a=item(comp,map);
				match(GT);
				b=item(comp,map);
				
						result=Double.parseDouble(a)>Double.parseDouble(b);
					
			}
			else if ((LA(1)==STRING||LA(1)==ID) && (LA(2)==GE)) {
				a=item(comp,map);
				match(GE);
				b=item(comp,map);
				
						result=Double.parseDouble(a)>=Double.parseDouble(b);
					
			}
			else if ((LA(1)==STRING||LA(1)==ID) && (LA(2)==LT)) {
				a=item(comp,map);
				match(LT);
				b=item(comp,map);
				
						result=Double.parseDouble(a)<Double.parseDouble(b);
					
			}
			else if ((LA(1)==STRING||LA(1)==ID) && (LA(2)==LE)) {
				a=item(comp,map);
				match(LE);
				b=item(comp,map);
				
						result=Double.parseDouble(a)<=Double.parseDouble(b);
					
			}
			else {
				throw new NoViableAltException(LT(1), getFilename());
			}
			
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_1);
		}
		return result;
	}
	
	public final String  item(
		HTMLBasicComponent comp,Map map
	) throws RecognitionException, TokenStreamException {
		String result="";
		
		Token  v1 = null;
		Token  v2 = null;
		
		try {      // for error handling
			switch ( LA(1)) {
			case STRING:
			{
				v1 = LT(1);
				match(STRING);
				result = getStringValue(v1.getText());
				break;
			}
			case ID:
			{
				v2 = LT(1);
				match(ID);
				result = getValue(comp, map,v2.getText());
				break;
			}
			default:
			{
				throw new NoViableAltException(LT(1), getFilename());
			}
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			recover(ex,_tokenSet_2);
		}
		return result;
	}
	
	
	public static final String[] _tokenNames = {
		"<0>",
		"EOF",
		"<2>",
		"NULL_TREE_LOOKAHEAD",
		"\"and\"",
		"\"or\"",
		"EQUAL",
		"NOTEQUAL",
		"GT",
		"GE",
		"LT",
		"LE",
		"STRING",
		"ID",
		"WS"
	};
	
	private static final long[] mk_tokenSet_0() {
		long[] data = { 2L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_0 = new BitSet(mk_tokenSet_0());
	private static final long[] mk_tokenSet_1() {
		long[] data = { 50L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_1 = new BitSet(mk_tokenSet_1());
	private static final long[] mk_tokenSet_2() {
		long[] data = { 4082L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_2 = new BitSet(mk_tokenSet_2());
	
	}
